//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_mousewheelrec.sqf"
#include "x_setup.sqf"

private ["_ct", "_role", "_rpic", "_t", "_ctrl", "_dospawn", "_crewok", "_sidepp"];
if (!alive player || {__pGetVar(xr_pluncon)}) exitWith {false};
_ct = if (vehicle player == player) then {
	cursorTarget
} else {
	vehicle player
};

if (isNull _ct || {!alive _ct} || {_ct distance player > 20} || {(!(_ct isKindOf "Car") && {!(_ct isKindOf "Tank")} && {!(_ct isKindOf "Air")})} || {_ct isKindOf "ParachuteBase"} || {_ct isKindOf "UAV_01_base_F"} || {_ct isKindOf "UGV_01_base_F"} || {_ct isKindOf "UAV_02_base_F"} || {getNumber(configFile/"CfgVehicles"/typeOf _ct/"isBicycle") == 1} || {(_ct call FUNC(GetAliveCrew)) == 0}) exitWith {false};
// TODO: Whenever countFriendly will get fixed so that it works with vehicle crew arrays too then change again
// if (player countFriendly (crew _ct) == 0) exitWith {false};
_sidepp = side (group player);
_crewok = false;
{
	if (alive _x && {_sidepp getFriend side (group _x) >= 0.6}) exitWith {
		_crewok = true;
	};
} forEach (crew _ct);
if (!_crewok) exitWith {false};

_ar_P = [];
_ar_AI = [];

{
	if (alive _x) then {
		if (isPlayer _x) then {
			_ar_P set [count _ar_P, _x];
		} else {
			_ar_AI set [count _ar_AI, _x];
		};
	};
	sideFriendly
} foreach (crew _ct);

_s_p = "";
if (count _ar_P > 0) then {
	_s_p = "<t align='left'>";
	{
		_role = assignedVehicleRole _x;
		if (count _role > 0) then {
			private "_rpic";
			if (commander _ct == _x) then {
				_rpic = __UI_Path_Roles(role_commander_ca.paa);
			} else {
				if (driver _ct == _x) then {
					_rpic = __UI_Path_Roles(role_driver_ca.paa);
				} else {
					_rpic = switch (toUpper (_role select 0)) do {
						case "TURRET": {__UI_Path_Roles(role_gunner_ca.paa)};
						default {__UI_Path_Roles(role_cargo_ca.paa)};
					};
				};
			};
			_s_p = _s_p + "<img image='" + _rpic + "'/> " + (name _x) + "<br/>";
		};
	} foreach _ar_P;
	_s_p = _s_p + "</t>";
};

_s_ai = "";
if (count _ar_AI > 0) then {
	_s_ai = "<t align='left'>";
	{
		_role = assignedVehicleRole _x;
		if (count _role > 0) then {
			private "_rpic";
			if (commander _ct == _x) then {
				_rpic = __UI_Path_Roles(role_commander_ca.paa);
			} else {
				if (driver _ct == _x) then {
					_rpic = __UI_Path_Roles(role_driver_ca.paa);
				} else {
					_rpic = switch (toUpper (_role select 0)) do {
						case "DRIVER": {__UI_Path_Roles(role_driver_ca.paa)};
						case "TURRET": {__UI_Path_Roles(role_gunner_ca.paa)};
						default {__UI_Path_Roles(role_cargo_ca.paa)};
					};
				};
			};
			_s_ai = _s_ai + "<img image='" + _rpic + "'/> " + (name _x) + " (AI)" + "<br/>";
		};
	} foreach _ar_AI;
	_s_ai = _s_ai + "</t>";
};

_t = "<t size='0.85'><t align='left'>" + (localize "STR_DOM_MISSIONSTRING_538") + " " + ([typeOf _ct, 0] call FUNC(GetDisplayName)) + ":</t>" + "<br/>" + _s_p + _s_ai + "</t>";
121282 cutRsc [QGVAR(rscCrewText), "PLAIN"];
_ctrl = __uiGetVar(GVAR(rscCrewText)) displayCtrl 9999;
_ctrl ctrlSetStructuredText parseText _t;
_ctrl ctrlCommit 0;
_dospawn = GVAR(rscCrewTextShownTimeEnd) == -1;
GVAR(rscCrewTextShownTimeEnd) = time + 5;
if (_dospawn) then {
	0 spawn {
		scriptName "spawn_crewrsc";
		private "_vecp";
		_vecp = vehicle player;
		waitUntil {sleep 0.221;time > GVAR(rscCrewTextShownTimeEnd) || {!alive player} || {__pGetVar(xr_pluncon)} || {vehicle player != _vecp}};
		121282 cutRsc ["XDEmpty", "PLAIN"];
		GVAR(rscCrewTextShownTimeEnd) = -1;
	};
};